package com.gxa.ssm251.controller;

import com.gxa.ssm251.pojo.Coupon;
import com.gxa.ssm251.pojo.vo.ManagePointsVo;
import com.gxa.ssm251.service.ManagePointsVoService;
import com.gxa.ssm251.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 舒空
 */
@RestController
@CrossOrigin
@RequestMapping("/managePointsVo")
@Api(value = "管理积分控制器", tags = "管理积分接口api")
public class ManagePointsVoController {

    @Resource
    ManagePointsVoService managePointsVoService;
    private JsonResult jsonResult = null;

    @ApiOperation(value = "分页逆序全查询")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "map", value = "json格式的积分对象page,size", paramType = "body")
    )
    @PostMapping("/selectAllByPage")
    public JsonResult selectAllByPage(@RequestBody Map<String, Object> map) {
        jsonResult = new JsonResult();
        int page = Integer.parseInt(map.get("page").toString());
        int size = Integer.parseInt(map.get("size").toString());
        page = (page - 1) * size;
        List<ManagePointsVo> selectAllByPage = managePointsVoService.selectAllByPage(page, size);
        List<ManagePointsVo> selectAll = managePointsVoService.selectAll();

        jsonResult.setCode("1");
        jsonResult.setCount(selectAll.size());
        jsonResult.setData(selectAllByPage);
        return jsonResult;
    }

    @ApiOperation(value = "姓名或手机模糊查询")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "map", value = "json格式的积分对象param", paramType = "body")
    )
    @PostMapping("/selectLikeNameOrPhone")
    public JsonResult method(@RequestBody Map<String, Object> map) {
        jsonResult=new JsonResult();
        String param = map.get("param").toString();
        List<ManagePointsVo> managePointsVos = managePointsVoService.selectLikeNameOrPhone(param);
        if (managePointsVos.size()>0){
            jsonResult.setCode("1");
            jsonResult.setData(managePointsVos);
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("查无此数据!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改管理积分的剩余积分")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "map", value = "json格式的积分对象surplus,id", paramType = "body")
    )
    @PostMapping("/update")
    public JsonResult update(@RequestBody Map<String, Object> map) {
        jsonResult = new JsonResult();
        ManagePointsVo managePointsVo = new ManagePointsVo();
        Double surplus = Double.valueOf(map.get("surplus").toString());
        Integer id = Integer.valueOf(map.get("id").toString());
        managePointsVo.setSurplus(surplus);
        managePointsVo.setId(id);
        ManagePointsVo selectById = managePointsVoService.selectById(id);
        jsonResult.setData(selectById);
        Integer update = managePointsVoService.update(managePointsVo);
        if (update > 0) {
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult;
    }
}
